unit graphSettingsGUI;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, GraphScale;

type
  TMethod = procedure of object;

  TGraphSettings = class(TForm)
    ColorDialog1: TColorDialog;
    Colours: TGroupBox;
    pbBackground: TPaintBox;
    Label1: TLabel;
    pbData: TPaintBox;
    Label2: TLabel;
    pbAxis: TPaintBox;
    Label3: TLabel;
    pbCursor: TPaintBox;
    Label4: TLabel;
    Axes: TGroupBox;
    Label5: TLabel;
    edOriginX: TEdit;
    edOriginY: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    edMajorTicksPer: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    edMinorTicksPer: TEdit;
    Label11: TLabel;
    GroupBox1: TGroupBox;
    Label12: TLabel;
    Label13: TLabel;
    edScaleX: TEdit;
    Label14: TLabel;
    edScaleY: TEdit;
    chkAutoscale: TCheckBox;
    Button1: TButton;
    Button2: TButton;
    Label15: TLabel;
    procedure pbBackgroundPaint(Sender: TObject);
    procedure pbAxisPaint(Sender: TObject);
    procedure pbDataPaint(Sender: TObject);
    procedure pbCursorPaint(Sender: TObject);
    procedure pbBackgroundClick(Sender: TObject);
    procedure pbAxisClick(Sender: TObject);
    procedure pbDataClick(Sender: TObject);
    procedure pbCursorClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
    _BackgroundColor: ^TColor;
    _ForegroundColor: ^TColor;
    _AxisColor:^TColor;
    _CursorColor : ^TColor;
	_AxisOrigin : ^TPoint;
	_Scale : ^TScale;
	_MajorTicksPer: ^Integer;
    _MinorTicksPer : ^Integer;
	_VerticalAutoscale : ^boolean ;
        
	procedure fillPaintbox(PaintBox : TPaintBox; Color : TColor);
	
  public
  InitialiseMethod : TMethod;
    Constructor CreateWithSettings(AOwner: TComponent;
	var BackgroundColor, ForegroundColor, AxisColor, CursorColor : TColor; 
	var AxisOrigin : TPoint; 
	var Scale : TScale; 
	var MajorTicksPer, MinorTicksPer : Integer; 
	var VerticalAutoscale : boolean);
	
	procedure UpdateBoxes;
  end;
{
var
  GraphSettings: TGraphSettings;}

implementation
{$R *.DFM}
	Constructor TGraphSettings.CreateWithSettings(AOwner: TComponent;
	var BackgroundColor, ForegroundColor, AxisColor, CursorColor : TColor; 
	var AxisOrigin : TPoint; 
	var Scale : TScale; 
	var MajorTicksPer, MinorTicksPer : Integer; 
	var VerticalAutoscale : boolean);
	
	begin
    Create(AOwner);
	_BackgroundColor := @BackgroundColor;
	_ForegroundColor := @ForegroundColor;
	_AxisColor := @AxisColor;
	_CursorColor := @CursorColor;
	
	_AxisOrigin := @AxisOrigin;
	_Scale := @Scale;
	_MajorTicksPer:= @MajorTicksPer;
	_MinorTicksPer := @MinorTicksPer;
	_VerticalAutoscale := @VerticalAutoscale;
	
	UpdateBoxes;
	end;
	
	procedure TGraphSettings.fillPaintbox(PaintBox : TPaintBox; Color : TColor);
	begin
		PaintBox.Canvas.Brush.Color := Color;
		PaintBox.Canvas.FillRect(PaintBox.ClientRect);
	end;
	

	procedure TGraphSettings.UpdateBoxes;
	begin
	pbBackground.Color := _BackgroundColor^;
	pbData.Color :=  _ForegroundColor^;
	pbAxis.Color := _AxisColor^;
	pbCursor.Color := _CursorColor^;
	
	edOriginX.Text := intToStr(_AxisOrigin^.x);
	edOriginY.Text := intToStr(_AxisOrigin^.y);
	
	edScaleX.Text := floatToStr(_Scale^.x);
	edScaleY.Text := floatToStr(_Scale^.y);
	
	edMajorTicksPer.Text := intToStr(_MajorTicksPer^);
	edMinorTicksPer.Text := intToStr(_MinorTicksPer^);
	
	chkAutoscale.Checked := _VerticalAutoscale^;
	end;

procedure TGraphSettings.pbBackgroundPaint(Sender: TObject);
begin
	fillPaintbox(pbBackground, pbBackground.Color);
	
end;

procedure TGraphSettings.pbAxisPaint(Sender: TObject);
begin
   fillPaintbox(pbAxis, pbAxis.Color);
end;

procedure TGraphSettings.pbDataPaint(Sender: TObject);
begin
     fillPaintbox(pbData, pbData.Color);
end;

procedure TGraphSettings.pbCursorPaint(Sender: TObject);
begin
fillPaintbox(pbCursor, pbCursor.Color);
end;

procedure TGraphSettings.pbBackgroundClick(Sender: TObject);
begin
	ColorDialog1.Color := pbBackground.Color;
	if ColorDialog1.Execute then
	begin
		pbBackground.Color := ColorDialog1.Color;
	end;

end;

procedure TGraphSettings.pbAxisClick(Sender: TObject);
begin
	ColorDialog1.Color := pbAxis.Color;
	if ColorDialog1.Execute then
	begin
		pbAxis.Color := ColorDialog1.Color;
	end;
end;

procedure TGraphSettings.pbDataClick(Sender: TObject);
begin
	ColorDialog1.Color := pbData.Color;
	if ColorDialog1.Execute then
	begin
		pbData.Color := ColorDialog1.Color;
	end;
end;

procedure TGraphSettings.pbCursorClick(Sender: TObject);
begin
	ColorDialog1.Color := pbCursor.Color;
	if ColorDialog1.Execute then
	begin
		pbCursor.Color := ColorDialog1.Color;
	end;
end;

procedure TGraphSettings.Button2Click(Sender: TObject);
begin
   Self.Close;
end;

procedure TGraphSettings.Button1Click(Sender: TObject);
var mbResult : Integer;
begin
	_BackgroundColor^ := pbBackground.Color;
	_ForegroundColor^ :=  pbData.Color;
	_AxisColor^ := pbAxis.Color;
	_CursorColor^ := pbCursor.Color;
	
	try
	_AxisOrigin^.x := strToInt(edOriginX.Text);
	_AxisOrigin^.y := strToInt(edOriginY.Text);
	
	_Scale^.x := strToFloat(edScaleX.Text);
	_Scale^.y := strToFloat(edScaleY.Text);
	
	_MajorTicksPer^ := strToInt(edMajorTicksPer.Text);
	_MinorTicksPer^ := strToInt(edMinorTicksPer.Text)
	except
		on E : Exception do
		begin
			mbResult := Application.MessageBox(PChar('Description: '+E.Message), PChar('Exception Raised: '+E.ClassName), MB_RETRYCANCEL+MB_ICONEXCLAMATION);
		end;
	end;

	_VerticalAutoscale^ := chkAutoscale.Checked;
	mbResult := IDCANCEL;
	if (mbResult <> IDRETRY) then
	begin
	if Assigned(InitialiseMethod) then InitialiseMethod;
	Self.Close;
	end;
end;

end.
